//
//  ANAppointmentPrepareController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/22.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANAppointmentPrepareController.h"
#import "inputCell.h"
#import "MineInfoFooterView.h"
#import "ShopEntity.h"
#import "GoodsEntity.h"
#import "ANGoodsListController.h"
#import "ANDatePickerViewController.h"
#import "AppointmentVipInfoCell.h"

@interface ANAppointmentPrepareController ()<InputCellTableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) GoodsEntity *goodsEntity;

@property (nonatomic, copy) NSString *appointmentTime;

@property (nonatomic, copy) NSString *rem;

@property (nonatomic, strong) MineInfoFooterView *footer;

@end

@implementation ANAppointmentPrepareController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"预约信息";
}

- (void)setupSubViews
{
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[AppointmentVipInfoCell class] forCellReuseIdentifier:@"AppointmentVipInfoCell"];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.footer = [[MineInfoFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, 90)];
    self.footer.title = @"立即预约";
    self.tableView.tableFooterView = self.footer;
    WeakSelf
    // 修改信息
    self.footer.sureHandler = ^{
        [weakSelf requestAppointment];
    };
}

- (void)requestAppointment
{
    if (!self.appointmentTime || self.appointmentTime.length < 1) {
        [MBProgressHUD showError:@"请选择预约时间"];
        return;
    }
    if (!self.goodsEntity) {
        [MBProgressHUD showError:@"请选择预约项目"];
        return;
    }
    NSDictionary *appointRecord = @{
                                    @"memberId":UserUtils.shared.vipId,
                                    @"consumerId":self.shopEntity.orgId,
                                    @"goodsId":self.goodsEntity.goodsId,
                                    @"time":self.appointmentTime,
                                    @"rem":StringNone(self.rem),
                                    @"state":@"1"
                                    };
    [URLManager.shareManager postUrlStr:Api_Add_Appointment parameters:appointRecord success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                [MBProgressHUD showSuccess:@"预约成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }
    if (section == 1) {
        return 3;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        AppointmentVipInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppointmentVipInfoCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.vipCodeString = UserUtils.shared.vipCode;
        cell.vipTelString = UserUtils.shared.telString;
        return cell;
    }
    inputCell *cell = [tableView dequeueReusableInputCellWithIdentifier:@"inputCell"];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"预约项目";
            cell.placeHolderString = @"请选择需要预约的项目";
            cell.editable = NO;
            cell.text = self.goodsEntity.goodsName;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"预约时间";
            cell.placeHolderString = @"请选择预约时间";
            cell.editable = NO;
            cell.text = self.appointmentTime;
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"备注";
            cell.placeHolderString = @"请输入备注";
            cell.text = self.rem;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"预约门店";
            cell.editable = NO;
            cell.text = self.shopEntity.orgName;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"门店电话";
            cell.editable = NO;
            cell.text = self.shopEntity.telephone;
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"门店地址";
            cell.editable = NO;
            cell.text = self.shopEntity.address;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView updataText:(NSString *)text indexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 2) {
            self.rem = text;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectTextViewAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            ANGoodsListController *vc = [[ANGoodsListController alloc] init];
            vc.title = @"选择预约项目";
            vc.consumerId = self.shopEntity.orgId;
            vc.type = ANGoodsListControllerTypeAppointment;
            vc.didSelectGoodsHandler = ^(GoodsEntity * _Nonnull entity) {
                self.goodsEntity = entity;
                [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row == 1) {
            ANDatePickerViewController *vc = [[ANDatePickerViewController alloc] init];
            vc.selectDate = ^(NSDate * _Nonnull date) {
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
                self.appointmentTime = [dateFormatter stringFromDate:date];
                [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            };
            [vc presentBottomViewControllerFromController:self];
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 50;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
